import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class JdbcRealmTest {

    @Test
    public  void ss() {
        Md5Hash md5Hash = new Md5Hash("123456");
        System.out.println(md5Hash);
/*        Md5Hash md5Hash = new Md5Hash("123456","admin",1024);
        System.out.println(md5Hash);*/
/*        String hashAlgorithmName = "MD5";//加密方式
        Object crdentials = "123456";//密码原值
        Object salt = "amdin";//盐值
        int hashIterations = 1024;//加密1024次
        Object result = new SimpleHash(hashAlgorithmName,crdentials,salt,hashIterations);
        System.out.println(result);*/
    }


    //配置数据源
    DruidDataSource dataSource = new DruidDataSource();
    {
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/testShiro?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
    }
    @Test
    public void  testShiro(){

        JdbcRealm jdbcRealm = new JdbcRealm();
        jdbcRealm.setDataSource(dataSource);
        jdbcRealm.setPermissionsLookupEnabled(true);//开启权限,就必须要实现查询权限
        //自定义查询语句,shiro默认的查询格式是，字段同的话 就不用自定义
        String sql = "select pwd from users where name=?";
        jdbcRealm.setAuthenticationQuery(sql);
        String roles="select role_name from role where username=?";
        jdbcRealm.setUserRolesQuery(roles);
        String permissions = "select permission from permissions where rolename = ?";
        jdbcRealm.setPermissionsQuery(permissions);


        //1.构建管理器环境
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //将用户加入进去
        securityManager.setRealm(jdbcRealm);
        //直接MD5加密
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");//加密名字
        matcher.setHashIterations(1);//加密次数
        jdbcRealm.setCredentialsMatcher(matcher);
        //MD5加盐加密
        // TODO
//        SimpleHash simpleHash = new SimpleHash("md5","123456","admin",1024);
        //2.提交认证请求
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        //认证
        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        subject.login(token);
        System.out.println("isAuthenticated:"+subject.isAuthenticated());
        System.out.println("checkeRoles:"+subject.hasRole("admin1"));
        System.out.println("hasPermission:"+subject.isPermitted("user:delete"));
    }
}
